<?php

require_once(fvSite::$fvConfig->get("path.entity") . "fvroot.class.php");

abstract class fvUploaded extends fvRoot {

	const IMAGE_TYPE_NORMAL = 'normal';
	const IMAGE_TYPE_THUMB = 'thumb';
	const IMAGE_TYPE_SMALL = 'small';
	const IMAGE_TYPE_LARGE = 'large';

	const IMAGE_WIDTH = 0;
	const IMAGE_HEIGHT = 1;
	const IMAGE_DEMENTIONS = 3;

	function __construct($fields, $tableName, $keyName = "id") {
		parent::__construct($fields, $tableName, $keyName);
	}

	public function hasFile ($fieldName, $size = null) {
		if (is_null($size))
		return is_file($this->getImageDir().$this[$fieldName]);
		else return is_file($this->getImageDir().$this->getImageName($fieldName, $size));
	}

	public function getImageSize($fieldName, $size = self::IMAGE_TYPE_NORMAL, $idx = self::IMAGE_DEMENTIONS) {
		$info = getimagesize($this->getImageDir().$this->getImageName($fieldName, $size));
		return $info[$idx];
	}

	public function getImageDir($web = false) {
		$baseDir = '';
		if ($web)
		$baseDir = fvSite::$fvConfig->get('path.upload.web_root_dir');
		else $baseDir = fvSite::$fvConfig->get('path.upload.root_dir');
		$currentDir = $baseDir . fvSite::$fvConfig->get('path.entImages.'.$this->currentEntity) . str_pad($this->getPk(), 8, '0', STR_PAD_LEFT) . "/";
		if (!$web && !$this->isNew() && !is_dir($currentDir)) mkdir($currentDir, 0777);

		return $currentDir;
	}

	public function getXmlFileDir($web = false) {
		$baseDir = '';
		if ($web)
		$baseDir = fvSite::$fvConfig->get('path.upload.web_root_dir');
		else $baseDir = fvSite::$fvConfig->get('path.upload.root_dir');
		$currentDir = $baseDir . fvSite::$fvConfig->get('path.entFile.'.$this->currentEntity) . str_pad($this->getPk(), 8, '0', STR_PAD_LEFT) . "/";
		if (!$web && !$this->isNew() && !is_dir($currentDir)) mkdir($currentDir, 0777);

		return $currentDir;
	}

	public function Array2XML($elements_string){
		$xml_array = array();
		$elements_regex = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';
		$attributes_regex = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
		preg_match_all ($elements_regex, $elements_string, $elements_array);
		foreach ( $elements_array[1] as $e_key => $e_value ){
			if ( ($attributes_string = trim($elements_array[2][$e_key])) ){
				preg_match_all($attributes_regex, $attributes_string, $attributes_array);
				foreach ( $attributes_array[1] as $a_key => $a_value ){
					$xml_array[$a_key]["attributes"][$a_value] = $attributes_array[2][$a_key];
				}
			}
			if ( ($p = strpos($elements_array[3][$e_key], "<")) > 0 ){
				$xml_array[$e_key]["text"] = substr($elements_array[3][$e_key], 0, $p - 1);
			}
			if ( preg_match($elements_regex, $elements_array[3][$e_key]) ){
				$xml_array[$e_key]["elements"] = $this->Array2XML($elements_array[3][$e_key]);
			}
			else if ( isset($elements_array[3][$e_key]) ){
				$xml_array[$e_key]["text"] = $elements_array[3][$e_key];
			}
		}
		return $xml_array;
	}

	public function deleteImages($fieldName) {
		$imagePattern = $this[$fieldName];

		foreach (glob ($this->getImageDir() . "*$imagePattern") as $fileName) {
			unlink($fileName);
		}

		$this[$this->getImageField()] = '';
		$this->save();
	}

	public function getImageName($fieldName, $size = self::IMAGE_TYPE_NORMAL) {
		return $size . "_" . $this->getOrigFileName($fieldName);
	}

	public function getOrigFileName ($fieldName) {
		return $this[$fieldName];
	}

	public function saveUploadFile($fieldName, $origFile, $type='image') {
		$info = pathinfo($origFile);
		$fileName = $info['basename'];

		$this[$fieldName] = $fileName;
		if ($type == 'image')
			rename($origFile, $this->getImageDir() . $fileName);
		else
			rename($origFile, $this->getXmlFileDir() . $fileName);
	}
}

